-----1.取出队列id   通过队列的pop("lr",list_name)函数获得 直接返回id
-----1.1 id: 通过get_data()取出数据
-----1.2 验证数据有效性 check_data()
--     1.2.1 验证失败:保存数据库
--        1.2.1_1 保存成功:  删除队列 数据  del_data()
--        1.2.1_2 保存失败:  将数据继续插入队列
--
--     1.2.3:验证后置客户端:测试host,port是否连通
--      1.2.3_1:不连通  数据插入队列
--    1.3 连通: 发送http
--      1.3.1检查返回的res.body
--      1.3.2 检查异常 继续插入 队列 重新发送
--    1.4检查成功:检查code 0,-1,1 删除队列中的数据       多次插入不成功调用push("lr",id) 函数插入队列
--    1.4.1 检查异常:再次加入队列重新发送


---取host port
--通过host 取id
--




---------------加载通用函数begin---------------
local utils                    = require "gcs.utils"    --加载
local cjson                    = require "cjson"
local error_utils              = require "gcs.upload.error_utils"    --加载错误 类
local send_utils               = require "gcs.upload.send_utils"
local config_table             = ngx.shared.gcs_config            --定义gcs_config缓存
local count_table              = ngx.shared.gcs_count
---------------加载通用 函数end---------------
---------------加载业务函数 begin-------------
local http                     = require "resty.http"
local vss_method               = require "gcs.list.method"
---------------加载业务函数 end---------------


---------------加载系统配置参数 begin----------
local p_pop_lr                 = config_table:get("p_hz_pop_lr")

local cjson_queue              = config_table:get("p_queue")
local is_use_threads           = config_table:get("p_is_use_threads")  --未完成  0:启动 1:关闭
local threads_num              = config_table:get("p_send_queue_num")    --线程数 等于发送的队列数
local send_queue_num           = config_table:get("p_send_queue_num")    --未完成
local store_connect_timeout    = config_table:get("p_store_connect_timeout")         --加载storage连接时间
local store_keep_timeout       = config_table:get("p_store_keep_timeout")            --加载storage超时时间
local store_keep_count         = config_table:get("p_store_keep_count")
local p_hz_server_state        = config_table:get("p_hz_server_state")

local p_resend_num             = config_table:get("p_resend_num")
local p_push_lr                = config_table:get("p_hz_push_lr")
local qz_num                   = config_table:get("p_yx_queue_qz")    --权重
local model                    = "send"
--local lo_ip                    = "128.127.110.189"
local tb_insert                = table.insert
---------------加载系统配置参数 end----------



--local pcall = pcall

--function main()


--参数校验


local tb_parameters = {}
tb_parameters.p_pop_lr = p_pop_lr
tb_parameters.cjson_queue = cjson_queue
tb_parameters.is_use_threads = is_use_threads
tb_parameters.threads_num = threads_num
tb_parameters.send_queue_num = send_queue_num
tb_parameters.p_push_lr = p_push_lr
tb_parameters.qz_num = qz_num
tb_parameters.store_connect_timeout = store_connect_timeout
tb_parameters.store_keep_timeout = store_keep_timeout
tb_parameters.store_keep_count = store_keep_count
tb_parameters.p_hz_server_state = p_hz_server_state
tb_parameters.p_resend_num = p_resend_num
local check_parameters = {
  "p_pop_lr",                  "is_use_threads",                 "threads_num",
  "send_queue_num",                 "p_push_lr",                "qz_num",
  "cjson_queue",               "store_connect_timeout",         "store_keep_timeout",
  "store_keep_count",           "p_hz_server_state",            "p_resend_num"
}





local tb_value = {
  timeout = store_connect_timeout,
  keep_timeout = store_keep_timeout,
  keep_count = store_keep_count
}

local res,err,error_no = utils.check_parameter(tb_parameters,check_parameters)

error_utils.check_res(res,model,1,err,error_no)



local tb_return_send = {}
----------------------------获得要发送的 队列的id,q_name begin---
--tb_id_qname是一个table {{id="",q_name=""},{id="",q_name=""}}

utils.gcs_log("debug","get_tb_id_qname start" )
local tb_id_qname,err,error_no = send_utils.get_tb_id_qname(send_queue_num,qz_num,cjson_queue,p_pop_lr)
local tb_id_qname = error_utils.check_res(tb_id_qname,model,1,err,error_no)
utils.gcs_log("debug","get_tb_id_qname end" )

----------------------------获得要发送的 队列的id,q_name {id=q_name} end-----------------





----------------------------发送数据 begin-------------------------------------------------
--tb_return_send是一个table 返回已经发送数据
--{{vssid="",hikid="",code="",message="",data="",gfile1="",gfile2=""},{}}
if tonumber(is_use_threads) == 0  then     --多线程发送
  tb_return_send = send_utils.send_tb_threads(tb_id_qname,threads_num)
elseif tonumber(is_use_threads) == 1 then  --单线程发送
  utils.gcs_log("debug","send_tb_single start")
  tb_return_send = send_utils.send_tb_single(tb_id_qname)
  utils.gcs_log("debug","send_tb_single end")
end
----------------------------发送数据 end---------------------

----------------------------做业务逻辑begin-------------------------

--[[
if table == "1" then table = "upload" end
if table == "2" then table = "success" end
if table == "3" then table = "fails" end
--]]
--code=0 表示发送成功 插入队列 2014-09-16-ip
--code=-2 表示重发

utils.gcs_log("error",model.."tb_return_send"..cjson.encode(tb_return_send))
for _,v in pairs(tb_return_send) do

  if tonumber(v.code) == 0 then
    
    local date = os.date("%Y-%m-%d",ngx.time())
    utils.gcs_log("error",model.."tb_return_send"..date)
    local res,err = vss_method.push(p_push_lr,date.."-dellist",v.vssid)            
    if not res then
      utils.gcs_log("error",model.."tb_return_send"..err)
      --utils.gcs_log("error",model..",code = 0,del ssdb data is error,vssid:"..v.vssid..",hikid:"..v.hikid..",err:"..cjson.encode(err)..",code:"..tonumber(v.code))
    end
    
  elseif tonumber(v.code) == -1 then
    local tb_data = {}
    local cjson_v = cjson.encode(v)
    tb_data[v.vssid] = cjson_v
    local res,err = vss_method.save_data("3",tb_data)       --保存数据库 (ssdb)
    if not res then
    utils.gcs_log("error",model..",code = -1,save ssdb data is error,vssid:"..v.vssid..",hikid:"..v.hikid..",err:"..cjson.encode(err)..",code:"..tonumber(v.code))
    end
    local tb_del_data = {}
    tb_insert(tb_del_data,v.vssid)
    local res,err = vss_method.del_data(tb_del_data)            --删除队列数据 (ssdb)
    if not res then
      utils.gcs_log("error",model..",code = -1,del ssdb data is error,vssid:"..v.vssid..",hikid:"..v.hikid..",err:"..cjson.encode(err)..",code:"..tonumber(v.code))
    end
    --删除storage 

    local hz_storage_res = v.hz_storage
    local gfile1 = v.gfile1
    local gfile2 = ''
    if v.gfile2 then
      gfile2 = v.gfile2
    end
    --qz_storage_res,tb_value,qz_storage_tb
    local hz_storage_tb = {}
    hz_storage_tb.gfile1 = gfile1
    hz_storage_tb.gfile2 = gfile2
    local res,err = send_utils.del_storage(hz_storage_res,tb_value,hz_storage_tb)
    if not res then
      utils.gcs_log("error",model..",code = -1,del hz_storage is error,err:"..cjson.encode(err))
    else
      send_utils.del_hz_storage_num()   ---记录当天删除后置storage次数
    end

  elseif tonumber(v.code) == -2 then                      --重发    
   
        local res,err = vss_method.push(p_push_lr,v.q_name,v.vssid)
        --utils.gcs_log("debug",cjson.encode())
        if not res then
          --utils.gcs_log("error",model .. ",code = -2,push is error,vssid:"..v.vssid..",hikid:"..v.hikid..",err:"..cjson.encode(err)..",code:"..tonumber(v.code))
        end

  elseif tonumber(v.code) == 3 then
    local res,err = vss_method.push(p_push_lr,v.q_name,v.vssid) --重发
   
    if not res then
      utils.gcs_log("error",model .. ",code = 3,push is error,vssid:"..v.vssid..",hikid:"..v.hikid..",err:"..cjson.encode(err)..",code:"..tonumber(v.code))
    end
    --[[
    local tb_data = {}
    local cjson_v = cjson.encode(v)
    tb_data[v.vssid] = cjson_v
    local res,err = vss_method.save_data("3",tb_data)       --保存数据库 (ssdb)
    if not res then
    utils.gcs_log("error",model..",code = 3,save ssdb data is error,vssid:"..v.vssid..",hikid:"..v.hikid..",err:"..cjson.encode(err)..",code:"..tonumber(v.code))
    end
    --]]
  elseif tonumber(v.code) == 4 then
   local id_resend = count_table:get(v.vssid.."_"..v.hikid)

    if not id_resend then
      count_table:set(v.vssid.."_"..v.hikid,1)
      local res,err = vss_method.push(p_push_lr,v.q_name,v.vssid)
      --utils.gcs_log("debug",cjson.encode())
      if not res then
        utils.gcs_log("error",model .. ",code = 4,push is error,vssid:"..v.vssid..",hikid:"..v.hikid..",err:"..cjson.encode(err)..",code:"..tonumber(v.code))
      end
    else
      local _num = tonumber(id_resend) + 1
      count_table:set(v.vssid.."_"..v.hikid,_num)
      if tonumber(_num) > tonumber(p_resend_num) then
        local l_listname = send_utils.get_l_listname()
        local res,err = vss_method.push(p_push_lr,l_listname,v.vssid)
        if not res then
          utils.gcs_log("error",model .. ",code = 4,push l_listname is error,vssid:"..v.vssid..",hikid:"..v.hikid..",err:"..cjson.encode(err)..",code:"..tonumber(v.code))
        end
      else
        local res,err = vss_method.push(p_push_lr,v.q_name,v.vssid)
        --utils.gcs_log("debug",cjson.encode())
        if not res then
          utils.gcs_log("error",model .. ",code = 4,push is error,vssid:"..v.vssid..",hikid:"..v.hikid..",err:"..cjson.encode(err)..",code:"..tonumber(v.code))
        end
      end
    end
  elseif tonumber(v.code) == 5 then
    local res,err = vss_method.push(p_push_lr,v.q_name,v.vssid) --重发
    if not res then
      utils.gcs_log("error",model .. ",code = 5,push is error,vssid:"..v.vssid..",err:"..cjson.encode(err)..",code:"..tonumber(v.code))
    end
  elseif tonumber(v.code) == 6 then
    local res,err = vss_method.push(p_push_lr,v.q_name,v.vssid) --重发
    if not res then
      utils.gcs_log("error",model .. ",code = 6,push is error,vssid:"..v.vssid..",hikid:"..v.hikid..",err:"..cjson.encode(err)..",code:"..tonumber(v.code))
    end
  elseif tonumber(v.code) == -3 then
    local res,err = vss_method.push(p_push_lr,v.q_name,v.vssid) --重发
    if not res then
      utils.gcs_log("error",model .. ",code = -3,push is error,vssid:"..v.vssid..",hikid:"..v.hikid..",err:"..cjson.encode(err)..",code:"..tonumber(v.code))
    end
  elseif tonumber(v.code) == -5 then
    local tb_data = {}
    local cjson_v = cjson.encode(v)
    tb_data[v.vssid] = cjson_v
    local res,err = vss_method.save_data("3",tb_data)       --保存数据库 (ssdb)
    if not res then     --保存失败(插入队列重发)
      local res,err = vss_method.push(p_push_lr,v.q_name,v.vssid) --重发
      if not res then
        utils.gcs_log("error",model .. ",code = -5,push is error,vssid:"..v.vssid..",hikid:"..v.hikid..",err:"..cjson.encode(err)..",code:"..tonumber(v.code))
      end                         --插入队列重发                                       
    end
  end
end
-----------------------------做业务逻辑end------------------

--end


--local res,err = pcall(main)
--if not res then
  
--end






--[[
for id,list_name in pairs(tb_id) do

  --取ip数据
  --验证 端状态是否为空
  local host       --要发送的后置客户端ip      后置客户端状态 id_state
  local is_id = config_table:get("h_"..host)  --取后置客户端对应的id
  local str_state = config_table:get(is_id.."_state")     --取后置客户端端口状态
  --判断
  if str_state == 'on' then
                                 --执行后面业务

     else 
                         --插入队列重新发送     
  end
end
--]]